package cn.com.huzo.service.purchase.impl;

import cn.com.huzo.model.entities.purchase.PoNoticeEntity;
import huzofw.model.entities.paginator.Paginator;
import cn.com.huzo.repository.purchase.PoNoticeRepository;
import huzofw.service.impl.GenericManagerImpl;
import cn.com.huzo.service.purchase.PoNoticeManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 采购下单通知管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("PoNoticeManager")
public class PoNoticeManagerImpl extends GenericManagerImpl<PoNoticeEntity, String>
        implements PoNoticeManager {

    @Resource(name = "PoNoticeRepository")
    private PoNoticeRepository _dao;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchForPage(Map<String, Object> criterion) throws Exception {
        Map<String, Object> rtnValue = new HashMap();

        Long offset = (long) (MapUtils.getInteger(criterion, "Limit") * (MapUtils.getInteger(criterion, "PageIndex") - 1));
        long total = _dao.fetchForCount(criterion);

        if (total > 0) {
            if (offset >= total) {
                offset = (total - MapUtils.getInteger(criterion, "Limit")) < 0 ? 0 : (total - MapUtils.getInteger(criterion, "Limit"));
            }
            criterion.put("Offset", offset);
            List<PoNoticeEntity> itemList = _dao.fetchForPage(criterion);
            Paginator<PoNoticeEntity> paginator = new Paginator<>(itemList, offset, MapUtils.getInteger(criterion, "Limit"), total);

            rtnValue.put("TotalRecordCount", paginator.getTotal());
            rtnValue.put("TotalPages", paginator.getTotalPages());
            rtnValue.put("PageIndex", paginator.getPageIndex());
            rtnValue.put("DataList", paginator.getItemList());
            rtnValue.put("Success", true);
        } else {
            rtnValue.put("Success", false);
        }

        return rtnValue;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Integer setReceiptedFlag(String poNoticeId, Short mrpType) throws Exception {
        return _dao.setReceiptedFlag(poNoticeId, mrpType);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Integer setOverFlag(String poNoticeId, Short mrpType, String overFlag) throws Exception {
        return _dao.setOverFlag(poNoticeId, mrpType, overFlag);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public List<String> sendPoNotice(List<PoNoticeEntity> itemList) throws Exception {
        if (itemList.isEmpty()) {
            return new ArrayList();
        }

        for (int i = 0; i < itemList.size(); i++) {
            Map<String, Object> criterion = new HashMap();
            criterion.put("PurchasedBy", itemList.get(i).getPurchasedBy());
            criterion.put("MrpId", itemList.get(i).getMrpId());
            criterion.put("CreatedBy", itemList.get(i).getCreatedBy());
            criterion.put("UpdatedBy", itemList.get(i).getUpdatedBy());
            criterion.put("BillType", itemList.get(i).getBillType());
            _dao.sendPoNotice(criterion);
        }

        // 取得通知的采购担当
        return _dao.fetchPurchasedByName(itemList.get(0).getMrpId(), itemList.get(0).getBillType());
    }
}
